<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Contract extends CI_Controller {

	var $sidebardata = array('page'=>'contract');
	
	public function index()
	{
		$userid = $this->loginc->getUserId();
		$oldAuth = $this->auth->getUserAuth($userid);
		$this->sidebardata['userAuth'] = $oldAuth;
		//传入权限列表，用于控制topnav的显示
		$this->sidebardata['userAuth'] = $oldAuth;
		$this->page->showSide($this->sidebardata,'content/contract/main');
	}
	
	public function contlist()
	{
		$userid = $this->loginc->getUserId();
		$oldAuth = $this->auth->getUserAuth($userid);
		$this->sidebardata['userAuth'] = $oldAuth;

		if(isset($_POST['conEdit'])) {
			if(!isset($_POST['eContract_ID']) || !isset($_POST['eProject_Name']) || !isset($_POST['eClient']) || !isset($_POST['eBearer']) || !isset($_POST['eStartTime']) || !isset($_POST['eEndTime']) || !isset($_POST['eFundAmount']) || !isset($_POST['eFundType']) || !isset($_POST['eInOutMark']) || !isset($_POST['eFundSource']) || !isset($_POST['eIsDutyFree']) || !isset($_POST['eCopyAmount']) )
			{
				echo '<script>alert("带*号的为必填项");history.go(-1);</script>';
			}
			else {
				$Contract_ID = $_POST['eContract_ID'];
				$Project_Name = $_POST['eProject_Name'];
				$Client = $_POST['eClient'];
				$Bearer = $_POST['eBearer'];
				$StartTime = $_POST['eStartTime'];
				$EndTime = $_POST['eEndTime'];
				$FundAmount = $_POST['eFundAmount'];
				$FundType = $_POST['eFundType'];
				$InOutMark = $_POST['eInOutMark'];
				if(!isset($_POST['eFundCardNum']))
					$FundCardNum = null;
				else
					$FundCardNum = $_POST['eFundCardNum'];
				$FundSource = $_POST['eFundSource'];
				$IsDutyFree = $_POST['eIsDutyFree'];
				$CopyAmount = $_POST['eCopyAmount'];

				$sql="UPDATE contract SET 
				Project_Name='$Project_Name', Client='$Client', Bearer='$Bearer', StartTime='$StartTime', EndTime='$EndTime', FundAmount='$FundAmount', FundType='$FundType', InOutMark='$InOutMark', FundCardNum=".($FundCardNum == null ? null : $FundCardNum).", FundSource='$FundSource', IsDutyFree='$IsDutyFree', CopyAmount='$CopyAmount' WHERE Contract_ID='$Contract_ID'";
				$this->db->query($sql);
			}
		}


		if(isset($_POST['delete']) && isset($_POST['check']))
		{
			foreach ($_POST['check'] as $i=>$name){
				$this->docontract->delete($name);
			}
		}
		$this->page->showSide($this->sidebardata,'content/contract/contlist');
	}

	public function submit()
	{
		$userid = $this->loginc->getUserId();
		$oldAuth = $this->auth->getUserAuth($userid);
		$this->sidebardata['userAuth'] = $oldAuth;


		if(isset($_POST['insert'])) {
			if(!isset($_POST['Contract_ID']) || !isset($_POST['Project_Name']) || !isset($_POST['Client']) || !isset($_POST['Bearer']) || !isset($_POST['LeaderID']) || !isset($_POST['StartTime']) || !isset($_POST['EndTime']) || !isset($_POST['FundAmount']) || !isset($_POST['FundType']) || !isset($_POST['InOutMark']) || !isset($_POST['FundSource']) || !isset($_POST['IsDutyFree']) || !isset($_POST['CopyAmount']) )
			{
				echo '<script>alert("带*号的为必填项");history.go(-1);</script>';
			}
			else {
			$Contract_ID = $_POST['Contract_ID'];
			$Project_Name = $_POST['Project_Name'];
			$Client = $_POST['Client'];
			$Bearer = $_POST['Bearer'];
			$LeaderID = $_POST['LeaderID'];

			$sql = "select * from user where JobNo = '".$LeaderID."'";
			$res = $this->db->query($sql);
			if($res->num_rows() == 0)
			{
				echo '<script>alert("LeaderID does not exist");history.go(-1);</script>';
			}

			$StartTime = $_POST['StartTime'];
			$EndTime = $_POST['EndTime'];
			$FundAmount = $_POST['FundAmount'];
			$FundType = $_POST['FundType'];
			$InOutMark = $_POST['InOutMark'];
			if(!isset($_POST['FundCardNum']))
				$FundCardNum = null;
			else
				$FundCardNum = $_POST['FundCardNum'];
			$FundSource = $_POST['FundSource'];
			$IsDutyFree = $_POST['IsDutyFree'];
			$CopyAmount = $_POST['CopyAmount'];

			$sql = "insert into contract values('$Contract_ID', '$Project_Name', '$Client', '$Bearer', '$LeaderID', '$StartTime', '$EndTime', '$FundAmount', '$FundType', '$InOutMark', '".($FundCardNum == null ? null : $FundCardNum)."', '$FundSource', '$IsDutyFree', '$CopyAmount', null, '待审批')";
			$this->db->query($sql);
			}
		}

		$this->page->showSide($this->sidebardata,'content/contract/submit');
	}
	
	public function mylist()
	{	
		$userid = $this->loginc->getUserId();
		$oldAuth = $this->auth->getUserAuth($userid);
		$this->sidebardata['userAuth'] = $oldAuth;

		if(isset($_POST['conEdit'])) {
			if(!isset($_POST['eContract_ID']) || !isset($_POST['eProject_Name']) || !isset($_POST['eClient']) || !isset($_POST['eBearer']) || !isset($_POST['eStartTime']) || !isset($_POST['eEndTime']) || !isset($_POST['eFundAmount']) || !isset($_POST['eFundType']) || !isset($_POST['eInOutMark']) || !isset($_POST['eFundSource']) || !isset($_POST['eIsDutyFree']) || !isset($_POST['eCopyAmount']) )
			{
				echo '<script>alert("带*号的为必填项");history.go(-1);</script>';
			}
			else {
				$Contract_ID = $_POST['eContract_ID'];
				$Project_Name = $_POST['eProject_Name'];
				$Client = $_POST['eClient'];
				$Bearer = $_POST['eBearer'];
				$StartTime = $_POST['eStartTime'];
				$EndTime = $_POST['eEndTime'];
				$FundAmount = $_POST['eFundAmount'];
				$FundType = $_POST['eFundType'];
				$InOutMark = $_POST['eInOutMark'];
				if(!isset($_POST['eFundCardNum']))
					$FundCardNum = null;
				else
					$FundCardNum = $_POST['eFundCardNum'];
				$FundSource = $_POST['eFundSource'];
				$IsDutyFree = $_POST['eIsDutyFree'];
				$CopyAmount = $_POST['eCopyAmount'];

				$sql="UPDATE contract SET 
				Project_Name='$Project_Name', Client='$Client', Bearer='$Bearer', StartTime='$StartTime', EndTime='$EndTime', FundAmount='$FundAmount', FundType='$FundType', InOutMark='$InOutMark', FundCardNum=".($FundCardNum == null ? null : $FundCardNum).", FundSource='$FundSource', IsDutyFree='$IsDutyFree', CopyAmount='$CopyAmount' WHERE Contract_ID='$Contract_ID'";
				$this->db->query($sql);

				if($_POST['eStatus'] == "未通过") {
					$sql = "UPDATE contract SET Status = '待审批' WHERE Contract_ID='$Contract_ID'";
					$this->db->query($sql);
				}
			}
		}

		$userid = $this->loginc->getUserId();
		$oldAuth = $this->auth->getUserAuth($userid);
		if(isset($_POST['delete']) && isset($_POST['check']))
		{
			foreach ($_POST['check'] as $i=>$name){
				$this->docontract->delete($name);
			}
		}

		$this->page->showSide($this->sidebardata,'content/contract/mylist');
	}
	
	public function checklist()
	{
		$userid = $this->loginc->getUserId();
		$oldAuth = $this->auth->getUserAuth($userid);
		$this->sidebardata['userAuth'] = $oldAuth;
		


		if(isset($_POST['pass'])) {
			$StatusArr = array("待审批", "学院已审批", "学院定密已完成", "科研院审批中", "已获批");
			$Contract_ID = $_POST['eContract_ID'];
			$Status = $_POST['eStatus'];
			for($i = 0; $i < count($StatusArr); ++$i)
				if($StatusArr[$i] == $Status) {
					$NxtStatus = $StatusArr[$i+1];
					break;
			}
					
			$sql="UPDATE contract SET Status='".$NxtStatus."' WHERE Contract_ID='$Contract_ID'";
			$this->db->query($sql);

			if($Status == "学院已审批") {
				$sql="UPDATE contract SET SecClass='".$_POST['eSecClass']."' WHERE Contract_ID='$Contract_ID'";
				$this->db->query($sql);
			}
		}
		elseif(isset($_POST['nopass'])) {
			$Contract_ID = $_POST['eContract_ID'];
			$sql="UPDATE contract SET Status='未通过', SecClass=null WHERE Contract_ID='$Contract_ID'";
			$this->db->query($sql);
		}
		$this->page->showSide($this->sidebardata,'content/contract/checklist');
	}
	
}

/* End of file user.php */
/* Location: ./application/controllers/user.php */